/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.DataAccessLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author MARS_NHO
 */
public abstract class BaseSQLConnect {

    String DataBase = ""; // Ten DataBase muon ket noi toi.
    String Host = ""; // Thong tin Host cua Database Server.
    String Password = ""; // Thong tin mat khau cua Database Server.
    String UserName = ""; // Thong tin ten dang nhap cua DataBase Server.
    Connection connect = null; // Khoi tao Connect bang null.
    ResultSet result = null; // Khoi tao Result chua du lieu.
    Statement statement = null; // Khoi tao Statement de thuc thi.

    public BaseSQLConnect() {
    }
    /// KIỂM TRA TRẠNG THÁI CÁC RESULT, STATEMENT, CONNECT

    public void Close() throws SQLException {
        // Neu Result chua dong => dong result
        if (this.result != null && !this.result.isClosed()) {
            this.result.close();
            this.result = null;
        }
        // Neu statement chua dong => dong statement
        if (this.statement != null && !this.statement.isClosed()) {
            this.statement.close();
            this.statement = null;
        }
        // Neu connection chua dong => dong connection
        if (this.connect != null && !this.connect.isClosed()) {
            this.connect.close();
            this.connect = null;
        }
    }

    // Ham kiem tra xem Driver connect Access da san sang hay chua?
    protected abstract void driverTest() throws Exception;

    // Ham thuc thi cau lenh Select lay du lieu tu CSDL
    public ResultSet excuteQuery(String Query) throws Exception {
        try {
            // Thuc thi cau lenh
            this.result = getStatement().executeQuery(Query);
        } // Neu khong thanh cong nem loi ra ngoai
        catch (Exception e) {
            throw new Exception("Error: " + e.getMessage() + " - " + Query);
        }
        // Tra ket qua ra ngoai
        return this.result;
    }

    // Ham thuc thi cau lenh Insert, Delete, Update
    public int excuteUpdate(String Query) throws Exception {
        // Khai bao bien int luu tru ket qua tinh trang thu thi cau lenh SQL
        int res = Integer.MIN_VALUE;
        try {
            // Thuc thi cau lenh
            res = getStatement().executeUpdate(Query);
        } // Neu khong thanh cong nem loi ra ngoai
        catch (Exception e) {
            throw new Exception("Error: " + e.getMessage() + " - " + Query);
        } finally {
            // Dong ket noi
            this.Close();
        }
        // Tra ket qua ra ngoai
        return res;
    }

    // Ham lay Connection
    protected abstract Connection getConnect() throws Exception;

    // Tao Statement de thuc yhi cau lenh Query
    protected Statement getStatement() throws Exception {
        // Kiem tra statement neu =null hoac da dong
        if (this.statement == null ? true : this.statement.isClosed()) {
            //if (this.statement == null || this.statement.isClosed()){
            // Khoi tao 1 statement moi
            this.statement = this.getConnect().createStatement();
        }
        return this.statement;
    }
}
